<?php

namespace App\System\Log\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Log\Model\LogSms;

class LogSmsRepository extends Repository
{
    use SinglePattern;

    /*
     * 记录列表
     * $getAll=false:获取分页数据 true：获取全部数据
     */
    public function getList($startTime='',$endTime='',$getAll=false)
    {
        if($getAll){
            $data = LogSms::when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->get();
        }else{
            $data = LogSms::when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->paginate();
        }
        return $data;
    }

    /**
     * 发送日志
     * @param $data
     * @return $this|bool|\Illuminate\Database\Eloquent\Model
     * @author liujun
     */
    public function createLogSms($data)
    {
        $res = LogSms::create($data);
        if(!$res)
        {
            return false;
        }
        return $res;
    }

    /**
     * 更新日志
     * @param $set 要更新的数据
     * @param $id  要更新的数据ID
     * @return bool
     */
    public function updateLogSms($set,$id)
    {
        $res = LogSms::where('id',$id)->update($set);
        return $res;
    }
}